$(function() {
    var $tipsDiv = $('#tipsDiv');

    var tip = {
        show: function (message) {
            $tipsDiv.html(message);
            $tipsDiv.slideDown();
            setTimeout(function () {
                $tipsDiv.slideUp();
            }, 3000)
        },
        hide: function () {

        }
    };

	$('.flexslider').flexslider( {
		animation : "slide",
		start : function(slider) {
			//$('body').removeClass('loading');
		}
	});

    var message = {
        'info': function (message) {
            var n = noty({
                text        : message,
                type        : 'warning',
                dismissQueue: true,
                timeout     : 1000,
                layout      : 'topCenter',
                theme       : 'defaultTheme',
                maxVisible  : 10
            });
        },
        'warn': function (message) {
            var n = noty({
                text        : message,
                type        : 'warning',
                dismissQueue: true,
                timeout     : 1000,
                layout      : 'topCenter',
                theme       : 'defaultTheme',
                maxVisible  : 10
            });
        }
    };

    $('#book_btn').click(function() {
        var brandId = $("#brandId").val();
        var mid = $("#mid").val();
        var wxno = $("#wxno").val();

        var meal_num = $(".number-picker-num").text();
        //$("#meal_num").val();


        var booking_date = $("#booking_date").val();
        var category = $("select[name='category']").find("option:selected").val()

        var name = $("#name").val();
        var phone = $("#phone").val();

        if(!meal_num){
            message.warn("请输入就餐人数");
            return;
        }
        if(!booking_date){
            message.warn("请输入预定时间");
            return;
        }
        if(!name){
            message.warn("请输入预定姓名");
            return;
        }
        if(!phone){
            message.warn("请输入预定手机");
            return;
        }

        $.post($("#ctx").val() + "/mobileweb/dining/book", {brandId:brandId, mid:mid, wxno:wxno,meal_num:meal_num, booking_date:booking_date, category:category, name:name, phone:phone}, function(data) {
            if (data.status == 'OK') {
                window.location.href = _g.ok_link
            } else {
                message.info(data.message);
            }
        });
    });

    $('#phone-call').click(function () {
        var phone = $(this).data('phone');
        if (phone) {
            window.location.href = "tel://" + phone;
        }
    });

    numberPicker({selector:".number-picker",hiddenInputSelector:"#hiddenInput",min:1,max:10,initNum:2});

    function numberPicker(obj){
        var node = $(obj.selector);
        var input = $(obj.hiddenInputSelector);
        var min = obj.min;
        var max = obj.max;
        var initNum = obj.initNum;
        initNum = initNum<min?min:initNum;
        initNum = initNum>max?max:initNum;
        input.val(initNum);

        var html = "<div class='number-picker-reduce'>-</div><div class='number-picker-num'>1</div><div class='number-picker-plus'>+</div>";
        node.css("display","inline");
        var width = node.attr("width");
        node.html(html);
        $(".number-picker-num").width(width-$(".number-picker-reduce").width()-$(".number-picker-plus").width());
        $(".number-picker-num").text(initNum);
        $(".number-picker-reduce").click(function (){
            initNum--;
            initNum = initNum<min?min:initNum;
            $(".number-picker-num").text(initNum);
            input.val(initNum);
        });

        $(".number-picker-plus").click(function (){
            initNum++;
            initNum = initNum>max?max:initNum;
            $(".number-picker-num").text(initNum);
            input.val(initNum);
        });
    }
});


